package com.yf.controller;
import com.yf.redis.DistributedRedisLock;
import com.yf.service.TsUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 分布式Redis锁测试controller
 */
@Slf4j
@RestController
@RequestMapping("/lock")
public class LockTestController {

    private final String LOCK = "LOCK";

    @Resource
    private DistributedRedisLock distributedRedisLock;
    @Resource
    private TsUserService tsUserService;

    // 测试不释放锁
    @GetMapping("/testLock")
    public void testLock() {
//        for (int i = 0; i < 5; i++) {
//            new Thread(() -> {
//                distributedRedisLock.lock(LOCK,7L,1L, TimeUnit.SECONDS);
//            }).start();
//        }
        new Thread(() -> {
            Boolean b = distributedRedisLock.lock("yytl",2 ,10,TimeUnit.SECONDS);
            if(b){
                try {
                    System.out.println("执行线程1的逻辑");
                    Thread.sleep(1000*2);
                    System.out.println("逻辑1执行完成");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }finally {
                    if(b){
                        distributedRedisLock.unlock("yytl");
                    }
                }
            }
        }).start();
        new Thread(() -> {
            Boolean b = distributedRedisLock.lock("yytl",2 ,10,TimeUnit.SECONDS);
            if(b){
                try {
                    System.out.println("执行线程2的逻辑");
                    Thread.sleep(1000*2);
                    System.out.println("逻辑2执行完成");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }finally {
                    if(b){
                        distributedRedisLock.unlock("yytl");
                    }

                }
            }
        }).start();
        new Thread(()->{
            int i = 0;
            while(i<10){
                i++;
                System.out.println(i);
            }
        }).start();
    }

    // 实际业务开发使用分布式锁的方式
    @PostMapping
    public void post() {
        try {
            if (distributedRedisLock.lock(LOCK)) {
                // 业务逻辑
                log.info("开始业务逻辑");
            } else {
                // 处理获取锁失败的逻辑
                log.info("获取锁失败");
            }
        } catch (Exception e) {
            log.error("处理异常：", e);
        } finally {
            distributedRedisLock.unlock(LOCK);
        }
    }

    @GetMapping("/bf")
    public void bf() {
//        log.info("rebel测试");
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();
        new Thread(() -> {
            tsUserService.addUser1("风风鱼鱼");
        }).start();

    }


}
